#include "ui/StartupDialog.h"
#include "ui/MainWindow.h"
#include "ui_StartupDialog.h"
#include <QStringList>
#include <QModelIndex>

StartupDialog::StartupDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::StartupDialog) {
    ui->setupUi(this);
    newNetworkDialog = NULL;
    joinSpecificDialog = NULL;

    const QStringList colorNames = QColor::colorNames();
    int index = 0;
    foreach (const QString &colorName, colorNames) {
        const QColor color(colorName);
        ui->colorComboBox->addItem(colorName, color);
        const QModelIndex idx = ui->colorComboBox->model()->index(index++, 0);
        ui->colorComboBox->model()->setData(idx, color, Qt::DecorationRole);
    }
}

StartupDialog::~StartupDialog() {
    delete ui;
}

void StartupDialog::on_buttonBox_accepted() {
    if (ui->firstNodeRadioButton->isChecked()) {
        newNetworkDialog = new NewNetworkDialog();
        int returnCode = newNetworkDialog->exec();

        if (returnCode == QDialog::Rejected) {
            setResult(QDialog::Rejected);
        }
    }
    else if (ui->joinSpecificRadioButton->isChecked()) {
        joinSpecificDialog = new JoinSpecificDialog();
        int returnCode = joinSpecificDialog->exec();

        if (returnCode == QDialog::Rejected) {
            setResult(QDialog::Rejected);
        }
    }
    else if (ui->localBroadcastRadioButton->isChecked()) {
        //TODO
        setResult(QDialog::Rejected);
    }
}

QString StartupDialog::getListenAddress() {
    return ui->addressLineEdit->text();
}

quint16 StartupDialog::getListenPort() {
    return ui->portSpinBox->value();
}

QColor StartupDialog::getPenColor() {
    const QModelIndex idx = ui->colorComboBox->model()->index(ui->colorComboBox->currentIndex(), 0);
    return qvariant_cast<QColor>(ui->colorComboBox->model()->data(idx, Qt::DecorationRole));
}
